//
//  ICTextField.swift
//  Dawn
//
//  Created by cuicheng on 2021/9/26.
//

import UIKit

public class ICTextField: UITextField {

    var insets: UIEdgeInsets = .zero
    var leftViewWidth: CGFloat = 0
    var rightViewWidth: CGFloat = 0

    public override func leftViewRect(forBounds bounds: CGRect) -> CGRect {
        return CGRect(x: insets.left,
                      y: insets.top,
                      width: leftViewWidth,
                      height: bounds.height-insets.vertical)
    }
    
    public override func rightViewRect(forBounds bounds: CGRect) -> CGRect {
        return CGRect(x: bounds.width-insets.right-rightViewWidth,
                      y: insets.top,
                      width: rightViewWidth,
                      height: bounds.height-insets.vertical)
    }
    
    public override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
        return CGRect(x: leftViewWidth + insets.left,
                      y: insets.top,
                      width: bounds.width - leftViewWidth - rightViewWidth - insets.horizontal,
                      height: bounds.height - insets.vertical)
    }
    
    public override func textRect(forBounds bounds: CGRect) -> CGRect {
        return CGRect(x: leftViewWidth + insets.left,
                      y: insets.top,
                      width: bounds.width - leftViewWidth - rightViewWidth - insets.horizontal,
                      height: bounds.height - insets.vertical)
    }
    
    public override func editingRect(forBounds bounds: CGRect) -> CGRect {
        return CGRect(x: leftViewWidth + insets.left,
                      y: insets.top,
                      width: bounds.width - leftViewWidth - rightViewWidth - insets.horizontal,
                      height: bounds.height - insets.vertical)
    }
    
}


public class ICLimitTextField: ICTextField {
    var maxTextCount: Int = Int.max {
        didSet{
            if rightView == nil, rightViewMode != .never {
                rightView = rightLimitLbl
                rightLimitLbl.text = "0/\(maxTextCount)"
            }
            addTarget(self, action: #selector(textChanged), for: .editingChanged)
        }
    }
    
    lazy var rightLimitLbl: UILabel = {
        let lbl = UILabel()
        lbl.font = font
        lbl.textAlignment = .right
        lbl.textColor = .hex(0x999999)
        return lbl
    }()
    
    @objc private func textChanged() {
        if rightView == nil, rightViewMode != .never {
            rightView = rightLimitLbl
            rightLimitLbl.text = "0/\(maxTextCount)"
        }
        guard let str = text else {
            if rightView != nil {
                rightLimitLbl.text = "0/\(maxTextCount)"
            }
            return }
        if str.count > maxTextCount {
            if markedTextRange == nil {
                text = String(str.prefix(maxTextCount))
            }
        }
        
        if markedTextRange == nil {
            if rightView != nil {
                rightLimitLbl.text = "\(text?.count ?? 0)/\(maxTextCount)"
            }
        }
    }
}
